package cn.chencaiju.algorithm;

import org.junit.Test;

public class Multiply {
	/**
	 * 0 asii 48
	 *  9 asii 57
	 * @param num1
	 * @param num2
	 * @return
	 */
    public String multiply(String num1, String num2) {
    	if(num1.equals("0")||num2.equals("0"))
    		return "0";
    	int[] result=new int[num1.length()+num2.length()];
		int further=0;    	

    	for(int i=num2.length()-1;i>=0;i--) {
    		
    		int index=result.length-num2.length()+i;
    		if('0'==num2.charAt(i))continue;
    		for(int j=num1.length()-1;j>=0;j--) {
    			int product=(num2.charAt(i)-48)*(num1.charAt(j)-48);
    			result[index]+=product+further;
    			if(result[index]>=10) {
    				further=result[index]/10;
    				result[index]%=10;
    			}else {
    				further=0;
    			}
    			index--;
    			if(j==0) {
    				result[index]+=further;
    				further=0;
    			}
    		}
    	}
    	StringBuilder sb=new StringBuilder();
    	for(int i=0;i<result.length;i++) {
        	if(i==0&&result[i]==0)continue;
    		sb.append(result[i]);
    	}

		return sb.toString();
    }	
    @Test public void testMultiply() {
    	System.out.println(406*10088);
    	System.out.println(multiply("10088","406"));
    	

    }
}
